﻿using DAO.db;
using Entity.shangping;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace DAO.shangping
{
    /// <summary>
    /// 商品数据库操作类
    /// </summary>
    public class GoodsDao
    {
        /// <summary>
        /// 保存商品
        /// </summary>
        /// <param name="goods">商品对象</param>
        /// <returns>10表示保存成功，否则保存失败</returns>
        public int Save(Goods goods)
        {
            int ret = 0;
            if (string.IsNullOrEmpty(goods.ID))//如果没有ID，则表示添加
            {
                goods.ID = Guid.NewGuid().ToString();
                goods.Status = 10;
                using (EFDbContext dbContext = new EFDbContext())
                {
                    dbContext.Goodss.Add(goods);
                    dbContext.SaveChanges();
                    ret = 10;
                }
            }
            else//否则表示修改
            {
                using (EFDbContext dbContext = new EFDbContext())
                {
                    dbContext.Entry(goods).State = EntityState.Modified;
                    dbContext.SaveChanges();
                    ret = 10;
                }
            }
            return ret;
        }

        /// <summary>
        /// 根据ID查询商品对象
        /// </summary>
        /// <param name="ID">商品ID</param>
        /// <returns></returns>
        public Goods GetByID(string ID)
        {
            Goods g = new Goods();
            using (EFDbContext dbContext = new EFDbContext())
            {
                g = dbContext.Goodss.Find(ID);
            }
            return g;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="goodsName">商品名称</param>
        /// <param name="goodsTypeID">商品分类ID</param>
        /// <returns>商品集合对象</returns>
        public List<Goods> GetList(string goodsName, string goodsTypeID)
        {
            List<Goods> ls = new List<Goods>();
            using (EFDbContext dbContext = new EFDbContext())
            {
                var query = dbContext.Goodss.AsQueryable();

                if (!string.IsNullOrEmpty(goodsName))
                {
                    query.Where(t => t.Name.Contains(goodsName));
                }
                if (!string.IsNullOrEmpty(goodsTypeID))
                {
                    query.Where(t => t.GoodsTypeID==goodsTypeID);
                }
                ls = query.ToList();
            }
            return ls;
        }

        /// <summary>
        /// 上架
        /// </summary>
        /// <param name="ID">商品ID</param>
        /// <returns>1表示上架成功，否则表示上架失败</returns>
        public int Up(string ID)
        {
            int ret = 0;
            using (EFDbContext dbContext = new EFDbContext())
            {
                var g = dbContext.Goodss.Find(ID);
                if (g != null)
                {
                    g.Status = 9;
                    dbContext.Entry(g).State = EntityState.Modified;
                    dbContext.SaveChanges();
                    ret = 1;
                }
            }

            return ret;
        }


        /// <summary>
        /// 下架
        /// </summary>
        /// <param name="ID">商品ID</param>
        /// <returns>1表示下架成功，否则表示下架失败</returns>
        public int Down(string ID)
        {
            int ret = 0;
            using (EFDbContext dbContext = new EFDbContext())
            {
                var g = dbContext.Goodss.Find(ID);
                if (g != null)
                {
                    g.Status = 10;
                    dbContext.Entry(g).State = EntityState.Modified;
                    dbContext.SaveChanges();
                    ret = 1;
                }
            }

            return ret;
        }


    }
}
